import suppliergoodsApi from "../services/suppliergoods"
export default {
    namespaced: true,
    state: {
        data: [],  //供应商货品数据
        total: 0,   //总条数
        condition: {  //模糊分页
            page: 1,
            limit: 3,
            field: "",
            sort: ""
        },
        editSuppliergoods: {} //当前正在修改
    },
    mutations: {
        mgetSuppliergoods(state, suppliergoods) { //所有货品
            state.data = suppliergoods;
        },
        settotal(state, total) { //设置总条数
            state.total = total;
        },
        setCondition(state, condition) {  //模糊查询分页
            state.condition = {
                ...state.condition,
                ...condition
            }
        },
        setEditSuppliergoods(state, suppliergoods) {    //设置正在修改
            state.editSuppliergoods = suppliergoods
        },
        deleteSupplier(state, id) {  //删除
            state.data = state.data.filter(it => it._id !== id)
        }
    },
    actions: {
        async agetSuppliergoods(context, id) {
            const data = await suppliergoodsApi.getSuppliergoods(context.state.condition, id);
            context.commit("mgetSuppliergoods", data.data); //触发事件改变
            context.commit("settotal", data.count) //设置总条数
        },
        async addSuppliergoods(context, suppliergoods) {
            await suppliergoodsApi.addSuppliergoods(suppliergoods)
        },
        async deleteSuppliergoods(context, id) {
            await suppliergoodsApi.deleteSuppliergoods(id);
            context.commit("deleteSupplier", id)
        },
        async setEditSuppliergoods(context, id) {
            const data = await suppliergoodsApi.getSuppliergoodsById(id);
            context.commit("setEditSupplier", data.data);
        },
        async modifySuppliergoods(context, s) {
            await suppliergoodsApi.modifySuppliergoods(s)
        }
    }
}